//
//  UserAccount.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/8/1.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit
//用户模型
class UserAccount: NSObject,NSCoding {
    
    class var userLogin: Bool {
        return loadAccount() != nil
    }
    /**
    "access_token" = "2.00IvNPLGss75UD3d71d10de00PwsSV";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 5662965122;
    */
    var access_token: String?
    var expires_in: NSTimeInterval = 0
    var expiresDate: NSDate?
    var uid: String?
    /// 用户图像
    var avatar_large: String?
    /// 用户名
    var name: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        
        //设置全局用户信息
        UserAccount.userAccount = self
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    override var description: String {
        let propperties = ["access_token","expires_in","uid","expiresDate","avatar_large","name"]
        
        return "\(dictionaryWithValuesForKeys(propperties))"
    }
    
    //MARK:-加载用户信息
    func loadUserInfo(finished: (error:NSError?) -> ()) {
        NetworkTools.sharedNetworkTools.loadUserInfo(uid!) { (result, error) -> () in
            if error != nil {
                finished(error: error)
                return
            }
            //设置用户信息
            self.name = result!["name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            
            //保存用户信息
            self.saveAccount()
            finished(error: nil)
            
        }
    }
    
    //归档和解档的方法
    //保存归档文件的路径
    static private let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("account.plist")
    
    func saveAccount() {
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath)
    }
    //设置静态账户属性
    private static var userAccount: UserAccount?
    class func loadAccount() -> UserAccount? {
     
        if userAccount == nil {
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
        }
        
        if let date = userAccount?.expiresDate where date.compare(NSDate()) == NSComparisonResult.OrderedAscending {
            userAccount = nil
        }
        
        return userAccount
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey:"access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
